<?php 
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
ob_start("ob_gzhandler");
require('../includes/init.php');
require('./classes/control/control_products.php');
require ('./configs/site_config.php');

class Products {
	
	var $products_c;
	var $webType;//网站类型
	
	function __construct() {
		
		$this->products_c = new ProductsControl();
		$this->webType = empty($_REQUEST['web_type']) ? 1 : (int) $_REQUEST['web_type'];
	}
	
	//商品管理
	function index() {
		
		$array_tag = Array (0 => Array ( tag_name => 'New' ,tag_id => 'New' ), 
							1 => Array ( tag_name => 'Free' , tag_id => 'Free' ),
							2 => Array ( tag_name => 'Upgrade' , tag_id => 'Upgrade' ),
							3 => Array ( tag_name => 'Top Seller' , tag_id => 'Top Seller' ),
							4 => Array ( tag_name => 'Top Downloader' , tag_id => 'Top Downloader' )
					) ;	
		//ajax修改
		if ($_GET['in_jax'] == 1) { 
			
			$field = '';
			switch ($_GET['type']) {
				
				case 'del'://修改删除标记
					$field = 'won_product_delete';
				break;  
				
				case 'state'://修改发布标记
					$field = 'won_product_state';
				break;
				
				case 'click_count'://修改发布标记
					$field = 'won_product_click';
				break;
				
				case 'update_tag'://修改tag
					$field = 'tag';
					$_GET['value']  =  substr($_GET['tag_str'], 0, strlen($_GET['tag_str']) - 1 );
				break;
				
				case 'p_sort'://修改排序
					$field = 'sort';
				break;
			} 
			
			if (!empty($_GET['p_id'])&&!empty($field)) {
				echo $this->products_c->change_info((int) $_GET['p_id'], $_GET['value'], $field,'one_update');
			}
		
		} elseif ($_GET['in_jax'] == 2) {//得到商品信息
			
			if (!empty($_GET['checkList'])) {
				
				$checkList = substr($_GET['checkList'], 0, (trim(strlen($_GET['checkList'])) - 1) );
				$this->products_c->change_info(0, $checkList, 'id', 'more_update');
			}
			
			if (!empty($_GET['p_id'])) {
				echo  $this->products_c->get_product_id('ajax', $_GET['p_id'], 0);
			}
			
		} elseif($_REQUEST['act'] == 'add') { //增加
			//提交表单操作
			if ($_POST['sub_from']) {
				
				$post_arr = $this->filterPost($_POST);
				
				//修改基础表成功
				$insert_id = $this->products_c->change_won_product($post_arr);
				if ($insert_id > 0) {
					
					$attributes = explode(',', $w_attributes_id);
					$sum = $this->change_product('add', $attributes,$insert_id);
				
					if ($sum > 0) {
						
						echo  '<script type="text/javascript"> 
						alert("增加成功！");
						window.location.href="/wondershare/products.php?action=index&web_type='.$this->webType.'";
						</script>';
					} else {
						echo  '<script type="text/javascript"> 
						alert("产品信息增加成功，你没有增加页面数据噢！");
						window.location.href="/wondershare/products.php?action=index&web_type='.$this->webType.'";
						</script>';
					}
				} else {
					echo  '<script type="text/javascript"> 
					alert("商品没有增加成功！");
					</script>';
				}
			}
			
			$GLOBALS['smarty']->assign('all_tag', $this->get_checkbox(array(0 => '0'), $array_tag, 'tag'));
			$GLOBALS['smarty']->assign('all_bar', $this->get_checkbox(array(0 => '0'), $this->products_c->get_all_bar(0, $this->webType), 'bar'));
			$GLOBALS['smarty']->assign('all_attributes', $this->get_checkbox(array(0 => '0'), $this->products_c->get_all_attributes(0, $this->webType), 'attributes'));
			$GLOBALS['smarty']->assign('web_info',   get_web_info($this->webType));
			$GLOBALS['smarty']->assign('web_type',     $this->webType);
			$GLOBALS['smarty']->assign('tile',     '增加');
			$GLOBALS['smarty']->assign('form_act',     'add');
			$GLOBALS['smarty']->assign('cat_select',   cat_list(0, 0, true, 0, true, '   WHERE c.cat_web = '.$this->webType.' AND c.cat_type = 2  ', 1));
			$GLOBALS['smarty']->assign('web_type', $this->webType);
			$GLOBALS['smarty']->display('products_info.htm'); 
		
		} else if ($_REQUEST['act'] == 'edit') { //修改
			
			if ($_POST['sub_from']) {
				//删除队列
				queueDelete(0, $_POST['ws_id']);
				
				$post_arr = $this->filterPost($_POST);
				
				//修改基础表成功
				$change_row = $this->products_c->change_won_product($post_arr);
				
				//修改基础表成功，以下判断是处理模板
				if ( $change_row > 0) {
					
					$w_attributes_array =  explode(',', $_POST['w_attributes_str']);
					$w_attributes_original_array =  explode(',', $_POST['w_attributes_original']);
					
					$update_array =  array_intersect($w_attributes_original_array, $w_attributes_array);
					$delete_array =  array_diff($w_attributes_original_array, $update_array);
					$add_array =  array_diff($w_attributes_array, $update_array);
				
					if (!empty($update_array)) {
						
						$this->change_product('update', $update_array ,$_POST['ws_id']);
					}
					
					if (!empty($add_array)) {     
						$this->change_product('add', $add_array, $_POST['ws_id']);
					}
					
					if (!empty($delete_array)) {
						$this->change_product('delete', $delete_array, $_POST['ws_id']);
					}
					
					
					//定义更新产品模板相关联的 id
					$update_arr = array('id' =>(int) $_POST['ws_id'],//won_product表id
										'w_type' => $this->webType,
										'operation_type' => 'product',
										'location' => '/wondershare/products.php?action=index&act=edit&web_type='.$this->webType.'&p_id='.$_POST['ws_id'],
					);
				
				 	echo '<script type="text/javascript">
						var ret = window.confirm("修改成功，是否生成其关联页面？\n\n确定：生成，取消：不生成");
						if( ret )
						{
							 window.location.href="/wondershare/template.php?action=updateTemplate&type=pub&'.http_build_query($update_arr).'";
						}else
						{
							 window.location.href="/wondershare/template.php?action=updateTemplate&type=update&'.http_build_query($update_arr).'";
						}
						</script>';
				} else {
					
					echo  '<script type="text/javascript"> 
					alert("产品没有修改成功！");
					window.location.href="/wondershare/products.php?action=index&act=edit&web_type='.$this->webType.'&p_id='.(int)$_POST['products_id'].'"; 
					</script>';
				}
			
			}
			
			$product_info = array();
			
			if (!empty($_GET['p_id'])) {
				//加入队列
				queueInfo($_GET['p_id']);
			
				$product_info = $this->products_c->get_product_id('edit',0,(int)$_GET['p_id'],$this->webType);
				$cat_select = cat_list(0, $product_info['cat_id'], true,0,true,'   WHERE c.cat_web = '.$this->webType.' AND c.cat_type = 2  ',1);
				$GLOBALS['smarty']->assign('all_tag', $this->get_checkbox(explode(',',$product_info['tag']),$array_tag,'tag'));
				$GLOBALS['smarty']->assign('cat_select',$cat_select  );
				$GLOBALS['smarty']->assign('product_info',     $product_info);
				$GLOBALS['smarty']->assign('all_bar', $this->get_checkbox(array(0=>'0'),$this->products_c->get_all_bar(0,$this->webType),'bar'));
				$GLOBALS['smarty']->assign('all_attributes', $this->get_checkbox(array(0=>'0'),$this->products_c->get_all_attributes(0,$this->webType),'attributes'));
			}
			
			$GLOBALS['smarty']->assign('form_act',     'edit');
			$GLOBALS['smarty']->assign('web_info',   get_web_info($this->webType));
			$GLOBALS['smarty']->assign('web_type',     $this->webType);
			$GLOBALS['smarty']->assign('tile',     '修改');
			$GLOBALS['smarty']->assign('web_type', $this->webType);
			$GLOBALS['smarty']->display('products_info.htm'); 
			
			
		} else {//列表
			
			$url_info = $products_info = array();
			
			//分页处理
			$type = !empty($_REQUEST['type']) ? $_REQUEST['type'] : 0;
			$page = 1;
			$page_size = 20;
			
			if ($type == 1) {
				$page_size = 160;
			}
			
			if (!empty($_REQUEST['page'])) {
				$page = (int) $_REQUEST['page'];
			}
			
			if ($_POST['search'] || !empty($_REQUEST['url_page']) || $_REQUEST['sortby'] ) {
				
				$_SESSION['fage'] = $_SESSION['fage'] == 1 ? $_SESSION['fage'] : 0;
				$pr_id	= !empty($_POST['pr_id']) ? $_POST['pr_id'] : $_GET['pr_id'];
				$pr_name = !empty($_POST['pr_name']) ? $_POST['pr_name'] : urldecode($_GET['pr_name']);
				$pr_name = $pr_name == '输入产品名称' ? '' : $pr_name;
			
				$url_info = $this->products_c->get_url_info($page, 'list_s', $page_size, $this->webType, $_REQUEST['cat_id'],
															$_REQUEST['is_delete'], $pr_id, $pr_name, $_REQUEST['tag'], $_REQUEST['is_emphasis'] ,$_REQUEST['sortby'], $_REQUEST['sort']);
				
				$products_info = $this->products_c->get_products_info('list_s', $url_info['page_info']['start'], $page_size, $this->webType,
																	$_REQUEST['cat_id'], $_REQUEST['is_delete'], $pr_id, $pr_name, $_REQUEST['tag'], 
																	$_REQUEST['is_emphasis'], $_REQUEST['sortby'], $_REQUEST['sort']);
				
				$GLOBALS['smarty']->assign('pr_id',   $pr_id);
				$GLOBALS['smarty']->assign('pr_name',   stripslashes($pr_name));
				$GLOBALS['smarty']->assign('cat_select_s',   cat_list(0, $_REQUEST['cat_id'], true, 0, true, '   WHERE c.cat_web = '.$this->webType.' AND c.cat_type = 2  '));
				$GLOBALS['smarty']->assign('tag',   $_REQUEST['tag']);
				$GLOBALS['smarty']->assign('is_emphasis',   $_REQUEST['is_emphasis']);
				$GLOBALS['smarty']->assign('sortby',   $_REQUEST['sortby']);
				$GLOBALS['smarty']->assign('sort',   $_REQUEST['sort']);
			} else {
				
				$url_info = $this->products_c->get_url_info($page,'list', $page_size, $this->webType);
				$products_info = $this->products_c->get_products_info('list', $url_info['page_info']['start'], $page_size, $this->webType);
			}
		
		
			if ($type == 1) {
				$product_listdata = !empty($_REQUEST['product_list']) ? explode(',', $_REQUEST['product_list']) : array();
			} else if($type == 2) {
				$product_listdata = !empty($_REQUEST['product_list']) ? $_REQUEST['product_list'] : 0;
			}
			
			$products_edit_id = !empty($_REQUEST['products_id']) ? $_REQUEST['products_id'] : 0;//此id是根据产品页面编辑页来的
			
			$GLOBALS['smarty']->assign('products_edit_id', $products_edit_id);
			$GLOBALS['smarty']->assign('product_listdata', $product_listdata);
			$GLOBALS['smarty']->assign('product_list', $_REQUEST['product_list']);
			$GLOBALS['smarty']->assign('type', $type);
			$GLOBALS['smarty']->assign('web_info',   get_web_info($this->webType));
			$GLOBALS['smarty']->assign('web_type',     $this->webType);
			$GLOBALS['smarty']->assign('page_url',   $url_info['page_url']);
			$GLOBALS['smarty']->assign('products_info', $products_info );
			$GLOBALS['smarty']->assign('total',   $url_info['page_info']['total']);
			$GLOBALS['smarty']->assign('cat_select',   cat_list(0, 0, true, 0, true, '   WHERE c.cat_web = '.$this->webType.' AND c.cat_type = 2  '));
			$GLOBALS['smarty']->assign('site_config', $GLOBALS['site_config'][1]['products'] );//$w_type = 1;
			$GLOBALS['smarty']->display('products_index.htm'); 
			
		}
	}

	/**
	* 得到checkbox组合
	*
	* @param   string        $attributes_id      产品属性
	* @param   string        $all_attributes     属性表所有属性
	* @param   string        $type               类型
	* @return  string
	*/
	function get_checkbox($select_arr_id, $all_checkbox, $type) {
		
		$str = '';
		$checkbox_name = '';
		$id = '';
		$name = '';
		
		if ($type == 'attributes') {
			$checkbox_name = "attributes[]";
			$id = 'id';
			$name = 'name';
		} elseif ($type == 'bar') {
			$checkbox_name = "bar[]";
			$id = 'block_id';
			$name = 'block_name';
		} elseif ($type == 'tag') {
			$checkbox_name = "tag[]";
			$id = 'tag_id';
			$name = 'tag_name';
		}
		
		for ( $i=0; $i < count($all_checkbox); $i++ ) {
			
			$fage = 1;
			foreach ($select_arr_id AS $key => $value) {
				
				if ($value == $all_checkbox[$i][$id]) {
				
					$fage = 0;
					$str .= '<input type="checkbox" name="'.$checkbox_name.'" value="'.$all_checkbox[$i][$id].'"  
							title = "'.htmlspecialchars($all_checkbox[$i][$name]).'" checked="checked"/>'.
							htmlspecialchars($all_checkbox[$i][$name]).' &nbsp;&nbsp;';
				}
			}
			
			if ($fage == 1) {
				
				$str .= '<input type="checkbox" name="'.$checkbox_name.'" value="'.
						$all_checkbox[$i][$id].'" title = "'.htmlspecialchars($all_checkbox[$i][$name]).'" />'.
						htmlspecialchars($all_checkbox[$i][$name]).' &nbsp;&nbsp;';
			}
		}
		
		return $str;
	
	}
	
	/**
	* 操作product_page表
	*
	* @param   string        $type               操作类型
	* @param   string        $change_array        数组
	* @param   int           $product_id          商品id
	* @return  int
	*/
	function change_product($type, $change_array, $product_id) {
		
		$sum = 0;
		if ($type == 'delete') {
			foreach ($change_array as $key => $value) {
				
				if (!empty($value)) {
					$sum += $this->products_c->change_product_page($type, $product_id, $value);
				}
			}
		} else {
			foreach ($change_array as $key => $value) {
				
				if (!empty($value)) {
				
					$sum += $this->products_c->change_product_page($type,$product_id,$value,
					trim($_POST['page_title'.$value]), trim($_POST['page_content'.$value]));
				}
			}
		}
		return $sum;
	}
	
	/**
	* 过滤post数据
	*
	* @return  string
	*/
	function filterPost($_POST) {
		
				
		$tag = !empty($_POST['tag']) ? implode(',', $_POST['tag']) : '';
		
		$w_attributes_id = substr($_POST['w_attributes_str'], -1) == ',' ? 
							substr($_POST['w_attributes_str'], 0, strlen($_POST['w_attributes_str']) - 1 ) : 
							$_POST['w_attributes_str'];
		
		$product_bar= substr($_POST['overview_bar_str'], -1) == ',' ? 
						substr($_POST['overview_bar_str'], 0, strlen($_POST['overview_bar_str']) - 1 ) : 
						$_POST['overview_bar_str'];
		
		$post_arr = array(  'act' => $_REQUEST['act'],
							'w_attributes_id' => $w_attributes_id,
							'guide_bar' => $product_bar,
							'techspec_bar' => $product_bar,
							'overview_bar' => $product_bar,
							'products_id' => $_REQUEST['act'] == 'edit' ? (int) $_POST['ws_id'] : (int) $_POST['hidden_products_id'] ,
							'parent_id' => (int) $_POST['parent_id'],
							'w_attributes_id' => $w_attributes_id,
							'tips' => trim($_POST['p_tips']),
							'tag' => $tag,
							'is_emphasis' => trim($_REQUEST['emphasis']),
							'top_image' => trim($_REQUEST['top_image']),
							'recommended_product' => $_REQUEST['h_recommended_product'],
							'is_high_version' => $_REQUEST['is_high_version'],
							'high_product' => $_REQUEST['h_high_product'],
							'advantages_content' => trim($_REQUEST['advantages_content']),
							'awards_image' => trim($_REQUEST['awards_image']),
							'giveaway' => $_REQUEST['giveaway'],
							'getcodeurl' => trim($_REQUEST['getcodeurl']),
							'is_review' => $_REQUEST['is_review'],
							'ad_guide' => trim($_REQUEST['ad_guide']),
							'overview_desc' => trim($_REQUEST['overview_desc']),
							'overview_img' => trim($_REQUEST['overview_img']),
							'overview_append' => trim($_REQUEST['overview_append']),
							'screenshots' => trim($_REQUEST['screenshots']),
							'webType' => $this->webType,
						);
		
		return $post_arr;
	}
}

$action    = $_REQUEST['action']; 
$products   = new Products;

if (method_exists($products, $action)) {
	$products->$action();
} else {
	$products->index();
}

	
?>
